package com.itheima.recommend.listener;

import com.alibaba.fastjson.JSON;
import com.itheima.recommend.config.RabbitConfig;
import com.itheima.recommend.pojo.Graph_product;
import com.itheima.recommend.util.SmartSearchUtil;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.impl.AMQImpl;
import org.aspectj.weaver.ast.Or;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

@Component
public class HistoryListener {
    @Autowired
    private RedisTemplate redisTemplate;

    @RabbitListener(queues = RabbitConfig.QUEUE_HISTORY)
    public void receiver(String message) {
        Graph_product graph = SmartSearchUtil.graph_product;
        Map map = JSON.parseObject(message, Map.class);
        String newProduct = (String) map.get("newProduct");
        List<String> oldList = (List<String>) map.get("oldList");
        if (oldList == null || oldList.size() == 0) {
            SmartSearchUtil.graph_product.index(newProduct);
//            SmartSearchUtil.addRoot(SmartSearchUtil.graph_product, newProduct);
            return;
        }
//        Graph_product graph = (Graph_product) redisTemplate.boundHashOps("Smart").get("history");
        for (String name : oldList) {
            if (name.equals(newProduct)) {
                continue;
            }
            //添加关系
            SmartSearchUtil.addEdge(graph, newProduct, name);
        }
//        redisTemplate.boundHashOps("Smart").put("history", graph);
    }
}
